using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace UserManagementService.Users
{
    /// <summary>
    /// 用户仓储接口
    /// </summary>
    public interface IUserRepository : IRepository<User, Guid>
    {
        /// <summary>
        /// 根据用户名查找用户
        /// </summary>
        Task<User?> FindByUserNameAsync(
            string userName,
            Guid? tenantId = null,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据邮箱查找用户
        /// </summary>
        Task<User?> FindByEmailAsync(
            string email,
            Guid? tenantId = null,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据手机号码查找用户
        /// </summary>
        Task<User?> FindByPhoneNumberAsync(
            string phoneNumber,
            Guid? tenantId = null,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        Task<bool> IsUserNameExistAsync(
            string userName,
            Guid? tenantId = null,
            Guid? excludeUserId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查邮箱是否存在
        /// </summary>
        Task<bool> IsEmailExistAsync(
            string email,
            Guid? tenantId = null,
            Guid? excludeUserId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查手机号码是否存在
        /// </summary>
        Task<bool> IsPhoneNumberExistAsync(
            string phoneNumber,
            Guid? tenantId = null,
            Guid? excludeUserId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据状态获取用户列表
        /// </summary>
        Task<List<User>> GetListByStatusAsync(
            UserStatus status,
            Guid? tenantId = null,
            bool includeDetails = false,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据用户类型获取用户列表
        /// </summary>
        Task<List<User>> GetListByUserTypeAsync(
            int userType,
            Guid? tenantId = null,
            bool includeDetails = false,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据角色ID获取用户列表
        /// </summary>
        Task<List<User>> GetListByRoleIdAsync(
            Guid roleId,
            Guid? tenantId = null,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据组织ID获取用户列表
        /// </summary>
        Task<List<User>> GetListByOrganizationIdAsync(
            Guid organizationId,
            Guid? tenantId = null,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取活跃用户列表
        /// </summary>
        Task<List<User>> GetActiveUsersAsync(
            Guid? tenantId = null,
            int maxCount = 100,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取最近登录用户列表
        /// </summary>
        Task<List<User>> GetRecentLoginUsersAsync(
            int days = 30,
            Guid? tenantId = null,
            int maxCount = 100,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取未登录用户列表
        /// </summary>
        Task<List<User>> GetInactiveUsersAsync(
            int days = 90,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 搜索用户
        /// </summary>
        Task<List<User>> SearchUsersAsync(
            string keyword,
            Guid? tenantId = null,
            bool includeDetails = false,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取用户统计信息
        /// </summary>
        Task<UserStatistics> GetStatisticsAsync(
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量更新用户状态
        /// </summary>
        Task<int> BatchUpdateStatusAsync(
            List<Guid> userIds,
            UserStatus status,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 删除指定时间的登录历史
        /// </summary>
        Task<int> DeleteLoginHistoriesBeforeAsync(
            DateTime beforeTime,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 删除指定时间的活动日志
        /// </summary>
        Task<int> DeleteActivityLogsBeforeAsync(
            DateTime beforeTime,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// 用户统计信息
    /// </summary>
    public class UserStatistics
    {
        /// <summary>
        /// 总用户数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 活跃用户数
        /// </summary>
        public int ActiveCount { get; set; }

        /// <summary>
        /// 待激活用户数
        /// </summary>
        public int PendingCount { get; set; }

        /// <summary>
        /// 禁用用户数
        /// </summary>
        public int DisabledCount { get; set; }

        /// <summary>
        /// 今日注册用户数
        /// </summary>
        public int TodayRegisteredCount { get; set; }

        /// <summary>
        /// 本月注册用户数
        /// </summary>
        public int ThisMonthRegisteredCount { get; set; }

        /// <summary>
        /// 今日登录用户数
        /// </summary>
        public int TodayLoginCount { get; set; }

        /// <summary>
        /// 本周登录用户数
        /// </summary>
        public int ThisWeekLoginCount { get; set; }

        /// <summary>
        /// 本月登录用户数
        /// </summary>
        public int ThisMonthLoginCount { get; set; }
    }
}